//
//  NoCardAppointmentRecordController.m
//  FQPub
//
//  Created by rzhy on 2017/9/14.
//  Copyright © 2017年 rzhy. All rights reserved.
//

#import "NoCardAppointmentRecordController.h"
#import <UIScrollView+EmptyDataSet.h>
#import "AppointmentRecordTableCell.h"
#import "Config.h"
#import "AbolishAppointmentController.h"

@interface NoCardAppointmentRecordController ()<UITableViewDelegate, UITableViewDataSource, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate, AbolishAppointmentControllerDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) UITextField  *tipTextField;
@property (nonatomic, strong) NSArray *dataArray;
@end

@implementation NoCardAppointmentRecordController
- (NSArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [[NSArray alloc] init];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"无法预约记录";
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame: CGRectZero];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"查询" style: UIBarButtonItemStyleDone target: self action:@selector(queryNoCardRecord)];
    
}

#pragma mark ---- 查询 ------
- (void)queryNoCardRecord
{
    [self showAlert];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.phone.length != 0) {
        [self getNoCardAppointmentListData];
    }else{
        [self showAlert];
    }
    

}

#pragma mark ---- 弹出输入框 ----
- (void)showAlert
{
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle: @"提示" message:@"请输入手机号进行查询" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *phoneAc = [UIAlertAction actionWithTitle:@"查询" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 获取输入框
        self.tipTextField = alertVc.textFields.firstObject;
        [self getNoCardAppointmentListData];
        
    }];
    
    [alertVc addAction: phoneAc];
    // 定义第一个输入框
    [alertVc addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请输入手机号";
    }];
    [self presentViewController: alertVc animated: YES completion: nil];
}

#pragma mark ----  获取数据  -------
- (void)getNoCardAppointmentListData {
    NSDictionary *cookie = @{CookieVaule : [UserDefaultsTool stringForKey:XHToken]};
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if (self.phone != 0) {
        [param setObject: self.phone forKey: @"dhhm"];
    }else{
        [param setObject: self.tipTextField.text forKey: @"dhhm"];
    }
    [HTTPSTool POSTRequestWithPath:XHMyOrder cookie:cookie params:param success:^(NSDictionary * _Nonnull JSON) {
                LRLog(@"我的预约记录 -> JSON: %@", JSON);
        _dataArray = JSON[@"data"][@"list"];
        [_tableView reloadData];
    }];
}
#pragma mark ------   AbolishAppointmentControllerDelegate  -----
- (void)abolisht:(AbolishAppointmentController *)abolish abolishAppointmentDataWithDataDictionary:(NSDictionary *)dataDict
{
    NSMutableArray *dataArray = [NSMutableArray arrayWithArray:_dataArray];
    [dataArray removeObject:dataDict];
    _dataArray = dataArray;
    [_tableView reloadData];
}


#pragma mark - tableView delegate/dataSource Method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    AppointmentRecordTableCell *cell = [AppointmentRecordTableCell createAppointmentRecordTableCellWithTableView: tableView];
//    NSDictionary *dict = self.dataArray[indexPath.row];
//    cell.dict = dict;
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return [[UITableViewCell alloc]init];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    AbolishAppointmentController *abolish = [[AbolishAppointmentController alloc] init];
    abolish.delegate = self;
    abolish.dataDict = _dataArray[indexPath.row];
    [self.navigationController pushViewController:abolish animated:YES];
}


#pragma mark - DZNEmptyDataSetSource Method
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    UIImage *image = [UIImage imageNamed:@"LoadFailure.png"];
    return image;
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *string = XHEmptyDes;
    NSAttributedString *abs = [[NSAttributedString alloc] initWithString:string attributes:nil];
    return abs;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -64.0f;
}

#pragma mark - DZNEmptyDataSetDelegate Method
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    [self showAlert];
}

@end
